<?php
class Companymodel extends Kmodel {
	function getAllCompany($user_id=null) {
		$sql = "SELECT com_id, com_code, com_name, com_desc, com_vat_num, com_address, com_phone, com_email, com_fax, com_number 
				FROM k_company 
				ORDER BY com_number ASC";
		return $this->db->query($sql);
	}
	
	function getCompanyByNumber($number) {
		$sql = "SELECT com_id, com_code, com_name, com_desc, com_vat_num, com_address, com_phone, com_email, com_fax, com_number 
				FROM k_company 
				WHERE com_number = '".pg_escape_string($number)."'";
		$query = $this->db->query($sql);
		$nb_rows = $query->num_rows();
		if ($nb_rows < 1) {
			return NULL;
		} else {
			return $query->row_array();
		}
	}
	
	function getCompanyById($id) {
		$sql = "SELECT com_id, com_code, com_name, com_desc, com_vat_num, com_address, com_phone, com_email, com_fax, com_number
				FROM k_company
				WHERE com_id = ".pg_escape_string($id);
		$query = $this->db->query($sql);
		$nb_rows = $query->num_rows();
		if ($nb_rows < 1) {
			return NULL;
		} else {
			return $query->row_array();
		}
	}
	
	function createCompany($data) {
		$com_id = $this->get_next_id("k_user_user_id_seq");
		$sql = "INSERT INTO k_company(	com_id, 
										com_code,
										com_name,
										com_desc,
				                        com_vat_num,
										com_address,
										com_phone,
										com_email,
										com_fax,
										com_number,
										user_create,
										date_create)
				             	VALUES(	".$com_id.",
				             		   	'".pg_escape_string($data["com_code"])."',
				             		   	'".pg_escape_string($data["com_name"])."',
				             		   	'".pg_escape_string($data["com_desc"])."',
				             		   	'".pg_escape_string($data["com_vat_num"])."',
				             		   	'".pg_escape_string($data["com_address"])."',
				             		   	'".pg_escape_string($data["com_phone"])."',
				             		   	'".pg_escape_string($data["com_email"])."',
				             		   	'".pg_escape_string($data["com_fax"])."',
				             		   	'".pg_escape_string($data["com_number"])."',
				             		   	".$data["user_create"].",
				             		   	CURRENT_TIMESTAMP
								)";
		$query = $this->db->query($sql);
		return $com_id;
	}
	
	function deleteCompany($com_id) {
		$sql = "DELETE FROM k_company WHERE com_id = ". $com_id;
		$this->db->query($sql);
	}
	
	function updateCompany($data) {
		$sql = "UPDATE k_company SET com_code = '".pg_escape_string($data["com_code"])."',
										com_name = '".pg_escape_string($data["com_name"])."',
										com_desc = '".pg_escape_string($data["com_desc"])."',
				                        com_vat_num = '".pg_escape_string($data["com_vat_num"])."',
										com_address = '".pg_escape_string($data["com_address"])."',
										com_phone = '".pg_escape_string($data["com_phone"])."',
										com_email = '".pg_escape_string($data["com_email"])."',
										com_fax = '".pg_escape_string($data["com_fax"])."',
										com_number = '".pg_escape_string($data["com_number"])."',
										user_update = ".$data["user_update"].",
										date_update = CURRENT_TIMESTAMP
							WHERE com_id = ".$data["com_id"];
		$this->db->query($sql);
	}
}